手機有各式大小、尺寸繁多,當然開發App也要做到Responsive響應式的設計,身為前端工程師,RWD網站可說是在熟悉不過,在Css裡設定好media query;或是在JS中判斷window尺寸,兩者都可以讓網站乖乖地不破版。那App該怎麼做呢?Flutter針對響應式介面提供了一些非常有用的widget:
BoxConstraints({double minWidth: 0.0, double maxWidth: double.infinity, double minHeight: 0.0, double maxHeight: double.infinity })
minWidth < BoxConstraints.width < maxWidth
minHeight < BoxConstraints.height < maxHeight
If the widget does not find a feasible size after consulting each constraint, the widget will eventually select a size for the child that meets the layout constraints but fails to meet the aspect ratio constraints.
如果widget在佈局中沒辦法找到匹配的寬高,則widget最終將為child選擇一個符合佈局約束但不符合長寬比的大小。
alignment
置左置中對齊。使用Boxfit來規定子元素如何縮放,他有許多參數可以使用,例如:fill 、cover、contain、fitWidth。MyRect(
child: FittedBox(
fit: Boxfit.contain,
alignment: Alignment.center
child: mychild()
)
)
Container(
alignment: Alignment.centerLeft, //在父層控制FractionallySizedBox位置
child: FractionallySizedBox( //FractionallySizedBox always align:center
widthFactor: 0.7, //70%
heightFactor: 0.8, //80%
child: MyChild(),
)
);
這邊有FractionallySizedBox的範例程式碼可以參考一下~
祝大家有個美好的國慶假期